﻿using System;
using System.Collections.Generic;
using System.Text;
using QuartzTypeLib;
using System.IO;
using System.Windows.Forms;


namespace Handrise.Mobile.BasicModal.Audio
{
    public enum AudioState { Init, Recording, RecordPause, Playing, PlayPause };

    public delegate void  PlayingEvent(int restSec,bool isStop);
    public delegate void RecordEvent(int recordSec, bool isStop);

    public class AudioService
    {
        private IWaveControl wave;
        private FilgraphManager m_objFilterGraph = null;
        private IMediaControl m_objMediaControl = null;
        private Timer playTimer = new Timer();
        private Timer recordTimer = new Timer();
        public event PlayingEvent OnPlaying;
        public event RecordEvent OnRecording;


        public AudioService()
        {
            playTimer.Interval = 1000;
            playTimer.Tick += new EventHandler(playTimer_Tick);
            playTimer.Enabled = false;

            recordTimer.Interval = 1000;
            recordTimer.Tick += new EventHandler(recordTimer_Tick);
            recordTimer.Enabled = false;
        }

        void playTimer_Tick(object sender, EventArgs e)
        {
            playSecCount --;

            if (OnPlaying != null)
                OnPlaying(playSecCount, false);

            if (playSecCount == 0)
            {
                StopPlay();
            }

        }

        void recordTimer_Tick(object sender, EventArgs e)
        {
            recordSec++;

            if (OnRecording != null)
                OnRecording(recordSec,false);
        }

        private AudioState recordState = AudioState.Init;
        /// <summary>
        /// 录音状态
        /// </summary>
        public AudioState RecordState
        {
            get
            {
                return recordState;
            }
        }

        private AudioState playState = AudioState.Init;
        /// <summary>
        /// 播放状态
        /// </summary>
        public AudioState PlayState
        {
            get
            {
                return playState;
            }
        }

        private Quality recordQuality = Quality.Lowest;
        /// <summary>
        /// 录音质量,目前只支持采样率最小的模式
        /// </summary>
        public Quality RecordQuality
        {
            get
            {
                return recordQuality;
            }
            set
            {
                recordQuality = Quality.Lowest ;
            }
        }
        /// <summary>
        /// 录音时间
        /// </summary>
        public TimeSpan RecordTimeSpan
        {
            get
            {
                if (wave != null)
                    return wave.RecordTimeSpan;
                else
                    return new TimeSpan();
            }
        }

        /// <summary>
        /// 开始录音
        /// </summary>
        /// <param name="filePath">存放路径</param>
        public void StartRecord(string filePath)
        {
            if (recordState == AudioState.Init)
            {
                wave = new Wave();
                wave.SavedFile = filePath;
                wave.RecordQuality = this.recordQuality;
                wave.Start();
                recordState = AudioState.Recording;

                recordSec = 0;
                recordTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 开始录音
        /// </summary>
        /// <param name="filePath">存放路径</param>
        /// <param name="recordQuality">录音质量</param>
        int recordSec = 0;
        public void StartRecord(string filePath, Quality recordQuality)
        {
            this.recordQuality = recordQuality;
            StartRecord(filePath);

        }
        /// <summary>
        /// 暂停录音
        /// </summary>
        public void PauseRecord()
        {
            if (wave == null)
                return;

            if (recordState == AudioState.Recording)
            {
                wave.Suspend();
                recordState = AudioState.RecordPause;

                recordTimer.Enabled = false;
            }
        }
        /// <summary>
        /// 继续录音
        /// </summary>
        public void ReStartRecord()
        {
            if (wave == null)
                return;

            if (recordState == AudioState.RecordPause)
            {
                wave.Continue();
                recordState = AudioState.Recording;

                recordTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 停止录音
        /// </summary>
        public void StopRecord()
        {
            if (wave == null)
                return;

            if (recordState == AudioState.Recording || recordState == AudioState.RecordPause)
            {
                wave.Stop();
                wave = null;
                recordState = AudioState.Init;

                recordTimer.Enabled = false;
                if (OnRecording != null)
                    OnRecording(recordSec,true);
            }
        }
        /// <summary>
        /// 开始播放
        /// </summary>
        /// <param name="filePath"></param>
        private int playSecCount;  //文件播放的秒数
        public int StartPlay(string filePath)
        {
            if (recordState != AudioState.Init)
                return 0;

            if (!File.Exists(filePath))
                return 0;
            FileInfo fileInfo = new FileInfo(filePath);
            playSecCount = (int)(fileInfo.Length / 1600);

            m_objFilterGraph = new FilgraphManager();
            m_objFilterGraph.RenderFile(filePath);
            if (m_objFilterGraph == null)
                return 0;

            m_objMediaControl = m_objFilterGraph as IMediaControl;
            m_objMediaControl.Run();
            playState = AudioState.Playing;
            playTimer.Enabled = true;

            return playSecCount;

        }

        /// <summary>
        /// 暂停播放
        /// </summary>
        public void PausePlay()
        {
            if (m_objMediaControl == null)
                return;
            if (playState == AudioState.Playing)
            {
                playState = AudioState.PlayPause;
                m_objMediaControl.Pause();
                playTimer.Enabled = false;
            }
        }
        /// <summary>
        /// 继续播放
        /// </summary>
        public void ReStartPlay()
        {
            if (m_objMediaControl == null)
                return;
            if (playState == AudioState.PlayPause)
            {
                playState = AudioState.Playing;
                m_objMediaControl.Run();
                playTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 停止播放
        /// </summary>
        public void StopPlay()
        {
            if (m_objMediaControl == null)
                return;
            if (playState == AudioState.PlayPause || playState == AudioState.Playing)
            {
                playState = AudioState.Init;
                m_objMediaControl.Stop();
                m_objMediaControl = null;

                m_objFilterGraph.Stop();
                m_objFilterGraph = null;
                GC.Collect();

                playTimer.Enabled = false;

                if (OnPlaying != null)
                    OnPlaying(0, true);

            }
        }
    }
}
